import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

declare var $: any;
@Component({
    selector: 'sa-device-off',
    templateUrl: './device-off.component.html',
    styleUrls:['./device-off.component.css']
})
export class DeviceOffComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	carList:any[] = []; //车辆数组
	selectedCompanyId:string = ''; //选中公司ID
	selectedCompanyName:string = ''; //选中公司name
	deviceId:string = '';//设备ID
	carNumber:string = ''; //车牌号码
	startTime:string = ''; //开始时间
	endTime:string = ''; //结束时间
	totalSecond:string = ''; //时长过滤参数
	advise:string = '';//异常处理意见
	rowId:string = '';//rowId
	abnormalType='';//警报类型
	inputCompanyTreeCarData:any = {};//车辆运行状态模块跳转该页面传递过来的参数
	constructor(private jsonApiService: JsonApiService,private router: Router,private activatedRoute: ActivatedRoute,) {
		activatedRoute.queryParams.subscribe(queryParams => {
			if(queryParams.nowPage){
				this.curPage = queryParams.nowPage;
			}
			this.inputCompanyTreeCarData = JSON.parse(common.getCookie('jumpAlertDataList'));
		});
	}
	//初始化时间
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
				()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						
						$("#datetimepicker1").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker2").datetimepicker("setStartDate", time);
						});
						$("#datetimepicker2").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker1").datetimepicker("setEndDate", time);
						});
						var date = new Date();
						$("#datetimepicker2").val(common.getTimeMmss(date));//当前时间
						date.setDate(date.getDate() - 7);//设置前一周
						$("#datetimepicker1").val(common.getTimeMmss(date));
						resolve();
					});
				}
			);
		})
	}
	//删除所选时间
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	ngOnInit() {
		if(common.getCookie('jumpAlertDataList')){
			this.abnormalType = this.inputCompanyTreeCarData.type;
			this.searchKey = this.inputCompanyTreeCarData.deviceId;
			this.initDate().then(()=>{
				$("#datetimepicker1").val(this.inputCompanyTreeCarData.startTime);
				$("#datetimepicker2").val(this.inputCompanyTreeCarData.endTime);
				this.startTime = $("#datetimepicker1").val();
				this.endTime = $("#datetimepicker2").val();
				this.load();
			})
			common.clearCookie('jumpAlertDataList');
		}else{
			this.initDate().then(()=>{
				this.load();
			})
		}
	}
	//  初始化加载list数据
	load() {
		this.startTime = $('#datetimepicker1').val();
		this.endTime = $('#datetimepicker2').val();
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				startTime:this.startTime,
				endTime:this.endTime,
				companyId:this.selectedCompanyId,
				deviceId:this.deviceId,
				totalSecond:this.totalSecond,
				type:this.abnormalType,
				searchKey:this.searchKey
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("alertlog/deviceShutDownAlarm.html", reqdata).subscribe(data => {
			if(!data.isSuccess){
				this.jsonApiService.message(data.message);
				return;
			}
			for(let item of data.exhibitDatas){
				item.totalSecond = common.secondToDate(item.totalSecond);
			}
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//异常处理
	dealMsg(row,val){
		this.rowId = row.id;
		val.show();
	}
	dealSubmit(val){
		let reqdata = "id="+this.rowId+"&addvice="+this.advise;
		this.jsonApiService.http("alertlog/updateDeviceShutDownAlarm.html", reqdata).subscribe(data => {
			if(data.success){
				val.hide();
			}
			this.jsonApiService.message(data.message);
			this.load();
		})
	}
	cancelDeal(val){
		val.hide();
	}
	ngOnDestroy(){
		document.onclick = null;
	}
	//刷新
    refresh(){
    	this.load();
    }
	//查询
	table_search(){
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
}
